package exampcom.qw.permissiontools.permission.bean;

import android.Manifest;
import android.content.pm.PackageManager;

import exampcom.qw.permissiontools.permission.debug.PermissionDebug;


/**
 * @author cd5160866
 * @date 2019/2/25
 */
public class Permission {

    private static final String TAG = "PermissionBean";
    /**
     * 权限名称
     */
    public String permissionName;

    /**
     * 授予结果
     */
    public int grantResult;

    /**
     * 是否需要给用户一个解释
     */
    public boolean shouldRationale;

    /**
     * 根据名称构建一个默认权限对象，默认失败
     *
     * @param permissionName 权限名称
     */
    public static Permission getDefault(String permissionName) {
        return new Permission(permissionName, PackageManager.PERMISSION_DENIED, false);
    }

    /**
     * 根据名称构建一个默认成功的权限对象
     *
     * @param permissionName 权限名称
     */
    public static Permission getDefaultSuccess(String permissionName) {
        return new Permission(permissionName, PackageManager.PERMISSION_GRANTED, false);
    }

    public Permission(String permissionName, int isGranted, boolean shouldRationale) {
        this.permissionName = permissionName;
        this.grantResult = isGranted;
        this.shouldRationale = shouldRationale;
    }

    public boolean isGranted() {
        return grantResult == PackageManager.PERMISSION_GRANTED;
    }

    @Override
    public String toString() {
        return permissionName + " isGranted: " + grantResult + " shouldRationale " + shouldRationale;
    }

    public String getPermissionNameDesc() {
        String desc;
        switch (permissionName) {
            case Manifest.permission.CAMERA:
                desc = "相机权限";
                break;
            case Manifest.permission.READ_CONTACTS:
            case Manifest.permission.WRITE_CONTACTS:
            case Manifest.permission.GET_ACCOUNTS:
                desc = "通讯录权限";
                break;
            case Manifest.permission.CALL_PHONE:
                desc = "电话权限";
                break;
            case Manifest.permission.BODY_SENSORS:
                desc = "传感器权限";
                break;
            case Manifest.permission.WRITE_EXTERNAL_STORAGE:
            case Manifest.permission.READ_EXTERNAL_STORAGE:
                desc = "存储权限";
                break;
            case Manifest.permission.ACCESS_FINE_LOCATION:
            case Manifest.permission.ACCESS_COARSE_LOCATION:
                desc = "定位权限";
                break;
            default:
                desc = "未知权限";
                PermissionDebug.e(TAG, desc);
                break;
        }
        return desc;
    }
}
